﻿using System;
using System.Collections.Generic;
using System.Data;

namespace ObjectManager
{
    public class Lop : ManagerComponent
    {
        private List<SinhVien> _sinhVien = new List<SinhVien>();

        public List<SinhVien> SinhVien
        {
            get { return _sinhVien; }
            set { _sinhVien = value; }
        }

        private LopDAO LopDAO { get; set; }

        public LopDTO ThongTin { get; set; }

        private void Initialize()
        {
            LopDAO = new LopDAO();
            ThongTin = new LopDTO();
            SinhVien = new List<SinhVien>();
        }

        public Lop()
        {
            Initialize();
        }

        public Lop(string Ten)
        {
            Initialize();
            ThongTin.Ten = Ten;
        }

        public Lop(string Ten, string BoMon)
        {
            Initialize();
            ThongTin.Ten = Ten;
            ThongTin.BoMon = BoMon;
        }

        public override int Add()
        {
            LopDAO.LopDTO = ThongTin;
            int check = LopDAO.Insert();
            if (check > 0)
            {
                foreach (SinhVien item in this.SinhVien)
                {
                    item.ThongTin.Lop = ThongTin.Ten;
                    check += item.Add();
                }
                return check;
            }
            return -1;
        }

        public override int Update()
        {
            LopDAO.LopDTO = ThongTin;
            return LopDAO.Update();
        }

        public override int Remove()
        {
            LopDAO.LopDTO = ThongTin;
            return LopDAO.Delete();
        }

        public List<string> ListName()
        {
            return LopDAO.ListName();
        }

        public List<string> ListName(string BoMon)
        {
            return LopDAO.ListName(BoMon);
        }

        public string FindName(string MaLop)
        {
            int Ma = 0;
            if (!string.IsNullOrEmpty(MaLop))
            {
                Ma = int.Parse(MaLop);
            }
            return LopDAO.FindName(Ma);
        }

        public DataTable ListAll()
        {
            return LopDAO.ListAll();
        }

        public DataTable ListAll(string Ma)
        {
            int ma = 0;
            if (!string.IsNullOrEmpty(Ma))
            {
                ma = int.Parse(Ma);
            }
            return LopDAO.ListAll(ma);
        }

        public DataTable ListAll(string Ten, string KyHieuBM, string BoMon)
        {
            if (string.IsNullOrEmpty(Ten))
            {
                Ten = "%";
            }
            else
            {
                Ten = "%" + Ten + "%";
            }
            if (string.IsNullOrEmpty(KyHieuBM))
            {
                KyHieuBM = "%";
            }
            else
            {
                KyHieuBM = "%" + KyHieuBM + "%";
            }
            if (string.IsNullOrEmpty(BoMon))
            {
                BoMon = "%";
            }
            else
            {
                BoMon = "%" + BoMon + "%";
            }
            return LopDAO.ListAll(Ten, KyHieuBM, BoMon);
        }

        public void Info(string Ma)
        {
            int ma = 0;
            if (!string.IsNullOrEmpty(Ma))
            {
                ma = int.Parse(Ma);
            }
            LopDAO.Search(ma);
            this.ThongTin = LopDAO.LopDTO;
        }

        public int Ma()
        {
            int result = 0;
            DataTable temp = LopDAO.Ma();
            if (temp != null && temp.Rows.Count > 0)
            {
                result = int.Parse(temp.Rows[0][0].ToString());
            }
            return result;
        }
    }
}
